package de.uni_mannheim.informatik.ai.dame_ai.util;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Window;
import java.io.File;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import de.uni_mannheim.informatik.ai.dame_ai.util.FileSystem.GameName;


@SuppressWarnings("serial")
public abstract class FileAccessDialog extends ModalDialog {

	protected JList saveList = new JList();
	protected JPanel buttonPan;
	protected JPanel panSouth = new JPanel();
	protected JTextField textField = new JTextField();
	protected JScrollPane scrollPane;
	protected File[] files;
	protected String[] savedGameReps;
	protected String[] savedGameNames;
	protected File returnFile;
	protected static final int WIDTH = 600;
	protected static final int HEIGHT = 500;
	private static final Pattern friendlyNamePat = Pattern.compile("(.+)\\..+\\.xml");
	private static final Pattern validFilenamePat = Pattern.compile("[^\\\\/:\\*\\?\"<>|]{1,128}");
	//The invalid Characters:   \ / : * ? " < > |
	private static final Pattern invalidCharPat = Pattern.compile("[\\\\*/*:*\\**\\?*\"*<*>*|*]");
	protected GameName game;

	
	public FileAccessDialog(Window owner,String title, FileSystem.GameName game)	{
		super(owner, title, WIDTH, HEIGHT);
		this.game=game;
		setResizable(false);
		files=FileSystem.getSaveFiles(game);
		savedGameReps=new String[files.length];
		savedGameNames=new String[files.length];
		if(files!=null)
			for(int i=0; i<files.length; i++){
				savedGameReps[i]=getFriendlyRep(files[i]);
				savedGameNames[i]=getFriendlyName(files[i]);
			}
		saveList.setListData(savedGameReps);
		scrollPane=new JScrollPane(saveList);
		this.setLayout(new BorderLayout());
		FlowLayout butPanLayout=new FlowLayout();
		butPanLayout.setHgap(20);
		buttonPan = new JPanel(butPanLayout);
		this.add(new PaddedPanel(scrollPane, 10, 0), BorderLayout.CENTER);
	}


	public static String getFriendlyName(File f) {
		Matcher mat=friendlyNamePat.matcher(f.getName());
		if(mat.find()){
			return mat.group(1);
		}
		else
			return f.getName();
	}

	public static String getFriendlyDate(File f) {
		DateFormat dateForm = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
		DateFormat timeForm= DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.getDefault());
		String friendlyDate=dateForm.format(new Date(f.lastModified()));
		String friendlyTime=timeForm.format(new Date(f.lastModified()));
		return friendlyDate+",  "+friendlyTime;
	}

	public static String getFriendlyRep(File f) {
		return "  "+getFriendlyName(f)+"  -  "+getFriendlyDate(f);
	}


	protected File getSelectedFile() {
		File gameDir=FileSystem.getDirectory(game);
		String fileName=textField.getText()+"."+game+".xml";
		return new File(gameDir, fileName);
		
//		File ret=null;
//		int selFileIndex=saveList.getSelectedIndex();
//		if(selFileIndex>=0)
//			ret=files[selFileIndex];
//		return ret;
	}

	protected boolean isValidFilename(String fName) {
		Matcher matcher = validFilenamePat.matcher(fName);
		return matcher.matches();
	}

	protected String getInvalidChar(String fName) {
		String invChar=null;
		Matcher matcher = invalidCharPat.matcher(fName);
		if(matcher.find())
			invChar = matcher.group();
		return invChar;
	}
}